
package com.mego.zbss.activity_fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.mego.zbss.R;
import com.mego.zbss.util.Constant;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.TopMenuWindow;
import zuo.biao.library.util.Log;
import zuo.biao.library.util.StringUtil;

/**
 * 通用网页Activity
 *
 * @author Lemon
 * @use toActivity(WebViewActivity.createIntent(...));
 */
public class SWebViewActivity extends BaseActivity implements OnBottomDragListener, OnClickListener, AlertDialog.OnDialogButtonClickListener {
    public static final String TAG = "SWebViewActivity";

    public static final String INTENT_RETURN = "INTENT_RETURN";
    public static final String INTENT_URL = "INTENT_URL";

    /**
     * 获取启动这个Activity的Intent
     *
     * @param title
     * @param url
     */
    public static Intent createIntent(Context context, String title, String url) {
        return new Intent(context, SWebViewActivity.class).
                putExtra(SWebViewActivity.INTENT_TITLE, title).
                putExtra(SWebViewActivity.INTENT_URL, url);
    }


    @Override
    public Activity getActivity() {
        return this;
    }

    private String mUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sweb_view_activity, this);//传this是为了全局滑动返回

        mUrl = StringUtil.getCorrectUrl(getIntent().getStringExtra(INTENT_URL));
        if (StringUtil.isNotEmpty(mUrl, true) == false) {
            Log.e(TAG, "initData  StringUtil.isNotEmpty(url, true) == false >> finish(); return;");
            enterAnim = exitAnim = R.anim.null_anim;
            finish();
            return;
        }

        tvBaseTitle.setText("钻豹视商-大陆");
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    private ProgressBar pbWebView;
    private WebView wvWebView;

    @Override
    public void initView() {
        autoSetTitle();

        pbWebView = (ProgressBar) findViewById(R.id.pbWebView);
        wvWebView = (WebView) findViewById(R.id.wvWebView);
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    @Override
    public void initData() {

        WebSettings webSettings = wvWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        wvWebView.requestFocus();

        // 设置setWebChromeClient对象
        wvWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
//                tvBaseTitle.setText(StringUtil.getTrimedString(title));
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pbWebView.setProgress(newProgress);
            }
        });

        wvWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                wvWebView.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
//                tvBaseTitle.setText(StringUtil.getTrimedString(wvWebView.getUrl()));
                pbWebView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                tvBaseTitle.setText(StringUtil.getTrimedString(wvWebView.getTitle()));
                pbWebView.setVisibility(View.GONE);
            }
        });

        wvWebView.loadUrl(mUrl);
    }


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initEvent() {

//        tvBaseTitle.setOnClickListener(this);
    }

    private static final String[] TOP_MENUS = {
            "商城入口", "入驻商户", "钻石商城", "团购", "优惠卷中心",
            "我的收藏", "申请入驻全返利商城", "切换至钻豹台湾商城"};

    private static final String[] TOP_MENUS_TW = {
            "商城入口", "入駐商戶", "鉆石商城", "團購", "優惠卷中心",
            "我的收藏", "申請入駐全返利商城", "切換至鉆豹大陸商城"};

    boolean _isTW = false;

    @Override
    public void onDragBottom(boolean rightToLeft) {
        if (rightToLeft) {
            if (wvWebView.canGoForward()) {
                wvWebView.goForward();
            }
            return;
        }
//		onBackPressed();
    }

    private void doChange(boolean isTW) {
        _isTW = isTW;
        mUrl = getWebByIndex(0);
        wvWebView.loadUrl(mUrl);
    }

    private String getWebByIndex(int index) {
        tvBaseTitle.setText("钻豹视商-大陆");
        String url = Constant.ZBSS_WEBSITE_0;
        switch (index) {
            default:
            case 0:
                break;
            case 1:
                url = Constant.ZBSS_WEBSITE_1;
                break;
            case 2:
                url = Constant.ZBSS_WEBSITE_2;
                break;
            case 3:
                url = Constant.ZBSS_WEBSITE_3;
                break;
            case 4:
                url = Constant.ZBSS_WEBSITE_4;
                break;
            case 5:
                url = Constant.ZBSS_WEBSITE_5;
                break;
            case 6:
                url = Constant.ZBSS_WEBSITE_6;
                break;
        }

        if (_isTW) {
            url = url.replace("mego_shopv2", "mego_shopv2_tw");
            url = url.replace("?i=12&", "?i=3&");
            tvBaseTitle.setText("鉆豹視商-臺灣");
        }

        return url;
    }

    @Override
    public void onReturnClick(View v) {
        finish();
    }

    @Override
    public void onForwardClick(View v) {
        if (_isTW) {
            toActivity(TopMenuWindow.createIntent(context, TOP_MENUS_TW), REQUEST_TO_TOP_MENU, false);
        } else {
            toActivity(TopMenuWindow.createIntent(context, TOP_MENUS), REQUEST_TO_TOP_MENU, false);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tvBaseTitle) {
            toActivity(EditTextInfoWindow.createIntent(context
                    , EditTextInfoWindow.TYPE_WEBSITE
                    , StringUtil.getTrimedString(tvBaseTitle)
                    , wvWebView.getUrl()),
                    REQUEST_TO_EDIT_TEXT_WINDOW, false);
        }
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void onBackPressed() {
        if (wvWebView.canGoBack()) {
            wvWebView.goBack();
            return;
        }

        super.onBackPressed();
    }

    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    protected void onPause() {
        super.onPause();
        wvWebView.onPause();
    }

    @Override
    protected void onResume() {
        wvWebView.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (wvWebView != null) {
            wvWebView.destroy();
            wvWebView = null;
        }
        wvWebView = null;
    }

    private long firstTime = 0;//第一次返回按钮计时

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK: {
                if (wvWebView.canGoBack()) {
                    wvWebView.goBack();
                    firstTime = 0;
                } else {
                    long secondTime = System.currentTimeMillis();
                    if (secondTime - firstTime > 2000) {
                        showShortToast("再按一次退出");
                        firstTime = secondTime;
                    } else {//完全退出
                        moveTaskToBack(false);//应用退到后台
                        System.exit(0);
                    }
                }
            }
            break;
        }
        return super.onKeyUp(keyCode, event);
    }

    protected static final int REQUEST_TO_EDIT_TEXT_WINDOW = 1;
    private static final int REQUEST_TO_TOP_MENU = 30;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            default:
            case REQUEST_TO_EDIT_TEXT_WINDOW:
                if (data != null) {
                    wvWebView.loadUrl(StringUtil.getCorrectUrl(data.getStringExtra(EditTextInfoWindow.RESULT_VALUE)));
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    int index = data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1);
                    if (index == 7) {
                        doChange(!_isTW);
                    } else if (index >= 0 && index < 7) {
                        mUrl = getWebByIndex(index);
                        wvWebView.loadUrl(mUrl);
                    }
                }
                break;
        }
    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }

        switch (requestCode) {
            default:
            case 0:
//				if (data != null) {
//					wvWebView.loadUrl(StringUtil.getCorrectUrl(data.getStringExtra(EditTextInfoWindow.RESULT_VALUE)));
//				}
                break;
        }
    }

    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}